//
//  TXController.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 15/11/29.
//  Copyright © 2015年 sgone. All rights reserved.
//

#import "TXController.h"
#import "ZHSetViewController.h"
#import "MSTXViewController.h"
#import "TXJLViewController.h"
#import "AFNetworking.h"

@interface TXController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)  AFHTTPSessionManager *manager;



@property(nonatomic,strong) NSMutableDictionary *list;


//判断是否进行过flag设置
@property(nonatomic,strong) NSString *flag;


@property(nonatomic,strong) NSString *existing;
@property(nonatomic,strong) NSString *has_apply;
@property(nonatomic,strong) NSString *could_apply;


@end

@implementation TXController



//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}


-(void)dealloc
{

    [[NSNotificationCenter defaultCenter]removeObserver:self];

}


- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.backBtn.hidden = NO;
//    self.title = @"账  户";
    self.titleLabel.text = @"账户设置";
    self.list = [NSMutableDictionary dictionaryWithCapacity:0];
    //赋初始值
    self.existing = @"0.0";
    self.has_apply = @"0";
    self.could_apply = @"0";
    
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(sendAPI) name:@"zhanghu" object:nil];
    
    //获取网络数据进行判断是否已经设置账户
    [self sendAPI];
    
    
}



#pragma mark 进行网路判断是否已经设置账户
-(void)sendAPI
{
    NSString *urlStr = [NSString stringWithFormat:@"%@get_account",SERVER_ADDRESS2];
    NSDictionary *params = @{@"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken};
    
    
    [self.manager POST:urlStr parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        
        if ([responseObject[@"error_flag"] intValue] == 0) {
            
          
         
            
            self.flag = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"account_info"][@"flag"]];
        
            self.list = [NSMutableDictionary dictionaryWithDictionary:responseObject[@"data"][@"account_info"]];
            
            self.existing =[self.list objectForKey:@"existing"];
            self.has_apply =[self.list objectForKey:@"has_apply"];
            self.could_apply = [self.list objectForKey:@"could_apply"];
            
            
            
            [self.tableView reloadData];
            
        }
        
        else
        {
            
            
            NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
            
        }
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
                UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"连接网络失败！" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
               [alter show];
        
    }];
    
    
    



}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{


    if (section == 0) {
        return 3;
        
    }
    else
    {
    
        return 1;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 50;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    
    if (section == 0) {
        
        
        return 0.1;
    }
   
    else
    {
        return 5;
    
    }

}






-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *Cellidentifier = @"Cell";
    UITableViewCell* cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cellidentifier];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.textLabel.font = [UIFont systemFontOfSize:16];
//    cell.textLabel.textColor = [UIColor grayColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth - 100, 0, 90, 50)];
    lbl.text = @"";
    [cell addSubview:lbl];
    
    
    
    
    if (indexPath.section == 0)
    {
    if (indexPath.row == 0)
    
    {
        cell.textLabel.text = @"账户余额";
        cell.accessoryType = UITableViewCellAccessoryNone;
        lbl.hidden = NO;
        lbl.text = [NSString stringWithFormat:@"%@元    ",[self.list objectForKey:@"could_apply"]];
        lbl.textColor = BenSe_COLOR;
        lbl.textAlignment = NSTextAlignmentRight;
        
    }
    else if (indexPath.row == 1)
    {
    cell.textLabel.text = @"马上提现";
        lbl.hidden = YES;
    
    }
    else
    {
    cell.textLabel.text = @"交易明细";
        lbl.hidden = YES;
    
    }
    
    }
    else if (indexPath.section == 1)
    {
    
        if (indexPath.row == 0) {
            
            cell.textLabel.text = @"提现设置";
            lbl.hidden = YES;
        }
    
    }
    
    
    
    
    return cell;
    

}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    
    if (indexPath.section == 0)

    {
        
        if (indexPath.row == 0) {
            
            
            return;
            
        }
        
        
        
     else if (indexPath.row == 1)
    {
    
        
        
        //判断有无进行过网络数据修改
        if ([self.flag isEqualToString:@"1"]) {
            
            
            MSTXViewController *tx = [[MSTXViewController alloc]initWithNibName:nil bundle:nil];
            [self.navigationController pushViewController:tx animated:YES];
            
            
            
        }
        else
        {
            [self.view makeToast:@"暂未进行银行账户设置"];
            
            
            
        }
        
    }
   
    else if (indexPath.row == 2)
    {
        

    
        TXJLViewController *jl = [[TXJLViewController alloc]initWithNibName:nil bundle:nil];
        
        //将这个页面请求的数据全部传递到交易记录里。里面包括总金额,
        
        jl.existing = self.existing;
        jl.has_apply = self.has_apply;
        jl.could_apply = self.could_apply;
        
        
        [self.navigationController pushViewController:jl animated:YES];
    
    
    }
    }
    
    if (indexPath.section == 1) {
        
        
        if (indexPath.row == 0) {
            
            
            ZHSetViewController *set = [[ZHSetViewController alloc]initWithNibName:nil bundle:nil];
            [self.navigationController pushViewController:set animated:YES];
            
        }
    }
    




}


@end
